package cn.java.dao.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import cn.java.entity.Husband;
import cn.java.entity.User;

public class One2ManyDaoImpl {
	
	private SqlSession session=null;
	
	@Before
	public void Init() throws IOException{
		//1.启动mybatis框架 
		//SqlSession <---------SqlSessionFactory <---------SqlSessionFactoryBuilder
		SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();
		//将mybatis.xml文件转化为输入流
		InputStream inputStream= Resources.getResourceAsStream("mybatis.xml");
		SqlSessionFactory ssf=sfb.build(inputStream);
		session=ssf.openSession();
	}
	
	@Test
	public void one2Many(){
		User user=session.selectOne("cn.java.dao.impl.One2ManyDaoImpl.one2Many");
		System.out.println(user);
	}
	
	//添加数据记录时返回主键值(使用实体类方式)
	@Test
	public void addUser(){
		User user=new User();
		user.setUsername("panyiwei");
		user.setPassword("144421");
		int result=session.insert("cn.java.dao.impl.One2ManyDaoImpl.addUser",user);
		session.commit();
		System.out.println("result="+result);//result表示的是影响的行数
		System.out.println("当前插入数据的主键为="+user.getId());
	}
	
	//添加数据记录时返回主键值(使用Map方式)
	@Test
	public void userAdd(){
		Map<String, Object> map=new HashMap<String, Object>();
		int result=session.insert("cn.java.dao.impl.One2ManyDaoImpl.userAdd",map);
		session.commit();
		System.out.println("result="+result);//result表示的是影响的行数
		System.out.println("当前插入数据的主键为="+map.get("id"));
	}
	
}
